<?php
declare (strict_types = 1);
/**
 * 素材模型
 */
namespace app\common\model;
use think\facade\Db;

class Advseq extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_adv_seq';

    public function advseqList($where = [], $offset = 0, $length = 20) {
        $field = ['a.*','t.channel_name'];

        if($length == 'all'){
          $list = self::field($field)
                  ->alias('a')
                  ->where($where)
                  ->join(['t_channel_info'=>'t'],'t_adv_seq.channel_id=t.id')
                  ->order(['s_time' => 'asc'])
                  ->select();
        }else{
          $list = self::field($field)
                  ->alias('a')
                  ->where($where)
                  ->limit(($offset) * $length, $length)
                  ->join(['t_channel_info'=>'t'],'t_adv_seq.channel_id=t.id')
                  ->order(['s_time' => 'asc'])
                  ->select();
        }
        
        $count = self::where($where)->count('adv_seq_id');

        // 数据处理
        foreach ($list as $key => $value) {
            // 判断周几可用
            $week_use = str_split_unicode($list[$key]['week_use']);
            sort($week_use);
            $week_attr= '';
            $week_array=array();
            for ($i=0; $i < count($week_use); $i++) { 
              if ($week_use[$i] ==7) {$week_attr.= "日,";$week_array[]="周日";}
              if ($week_use[$i] ==1) {$week_attr.= "一,";$week_array[]="周一";}
              if ($week_use[$i] ==2) {$week_attr.= "二,";$week_array[]="周二";}
              if ($week_use[$i] ==3) {$week_attr.= "三,";$week_array[]="周三";}
              if ($week_use[$i] ==4) {$week_attr.= "四,";$week_array[]="周四";}
              if ($week_use[$i] ==5) {$week_attr.= "五,";$week_array[]="周五";}
              if ($week_use[$i] ==6) {$week_attr.= "六,";$week_array[]="周六";}
            }
            $week_attr = substr($week_attr, 0, -1);
            $list[$key]['week_attr']  = $week_attr;
            $list[$key]['week_use']   = $week_array;
            // 转换成帧，1秒=25帧
            $list[$key]['s_time']  = gmdate('H:i:s', (int)$value['s_time']/25);
            $list[$key]['pgm_len'] = gmdate('H:i:s', (int)$value['pgm_len']/25)."-".(int)$value['pgm_len']%25;
            // 改变时间控制的类型
            $list[$key]['date_limit_s'] = $list[$key]['date_limit'] == 1 ? false : true;
            $list[$key]['time_status']  = $list[$key]['date_limit'] == 1 ? true : false;
            // 改变权限控制的类型
            $list[$key]['rights_limit_s'] = $list[$key]['rights_limit'] == 1 ? false : true;
            $list[$key]['rights_status']  = $list[$key]['rights_limit'] == 1 ? true : false;
        }
        return [
            'list'  => $list,
            'total' => $count
        ]; 
    }

    public function advseqBt($where) {
        $field = ['*'];

        $list = self::field($field)
            ->whereRaw($where)
            ->select();

        return $list;
    }

    // 查询节目有效期和当天是否有效
    public function advseqalive($param = []) {
        $sql = "select * from t_adv_seq where 1=1";
        if (!empty($param['channel_id'])){
            $sql .= " AND channel_id=".$param['channel_id'];
        }
        if (!empty($param['play_date'])){
            $sql .= " AND (date_limit=1 OR (date_limit=2 AND s_date <='".$param['play_date']."' AND e_date >='".$param['play_date']."'))";
        }
        $list = Db::query($sql);
        $data = [];
        // 数据处理
        foreach ($list as $key => $value) {
            // 判断周几可用
            $week_use = str_split_unicode($list[$key]['week_use']);
            sort($week_use);
            
            $week_attr= '';
            $week_array=array();
            for ($i=0; $i < count($week_use); $i++) { 
              if ($week_use[$i] ==7) {$week_attr.= "日,";$week_array[]="周日";}
              if ($week_use[$i] ==1) {$week_attr.= "一,";$week_array[]="周一";}
              if ($week_use[$i] ==2) {$week_attr.= "二,";$week_array[]="周二";}
              if ($week_use[$i] ==3) {$week_attr.= "三,";$week_array[]="周三";}
              if ($week_use[$i] ==4) {$week_attr.= "四,";$week_array[]="周四";}
              if ($week_use[$i] ==5) {$week_attr.= "五,";$week_array[]="周五";}
              if ($week_use[$i] ==6) {$week_attr.= "六,";$week_array[]="周六";}
            }
            $week_attr = substr($week_attr, 0, -1);
            $list[$key]['week_attr']  = $week_attr;
            $list[$key]['week_use']   = $week_array;
            // 转换成帧，1秒=25帧
            $list[$key]['s_time']  = gmdate('H:i:s', (int)$value['s_time']/25);
            // $list[$key]['pgm_len'] = gmdate('H:i:s', (int)$value['pgm_len']/25)."-".(int)$value['pgm_len']%25;
            $list[$key]['s_time_code'] = (int)$value['s_time'];
            $list[$key]['pgm_len_code'] = (int)$value['pgm_len']; //保留个时间帧字段方便其他地方调用此方法直接拿来计算使用。

            // 判断传过来的日期是否满足周几可用,不符合条件就删除此条时段记录
            if (!empty($param['play_date'])){
                $weeks_day = date('w', strtotime($param['play_date']));
                if(!in_array($weeks_day, $week_use)){
                    unset($list[$key]);
                }
            }

            if(isset($list[$key])) {
                $data[] = $list[$key];
            }
        }
        return [
            'list'  => $data
        ]; 
    }

}
